package com.zegome.game.gomestory.main.scene.play.board;

import java.util.Random;

import com.badlogic.gdx.math.Vector2;

public class MoveProcessor{
	
	Board board;
		
	float timeRun = 0.4f;
	
	int style1, style2, style3, style4;
	
	public void reset(){
		Random rand = new Random();
		style1 = rand.nextInt(4);
		style2 = rand.nextInt(4);
		style3 = rand.nextInt(4);
		style4 = rand.nextInt(4);
	}
	
	public MoveProcessor(Board board){
		this.board = board;
	}
	
	public void moveTemp(int style, int r0, int c0, int rMax, int cMax, int r1,int c1,int r2, int c2 ){
		switch (style) {
		case 0:
			move0(r0, c0, rMax, cMax, r1, c1, r2, c2);
			break;
		case 1:			
			move1(r0, c0, rMax, cMax, r1, c1, r2, c2);
			break;
		case 2:			
			move2(r0, c0, rMax, cMax, r1, c1, r2, c2);
			break;
		case 3:			
			move3(r0, c0, rMax, cMax, r1, c1, r2, c2);
			break;
		}
	}
	
	public void move(int r1, int c1, int r2, int c2)
	{	
		// Up
		if(board.dataMap.stylePlay == 0)
		{
			move0(1,1,board.dataMap.mapRow, board.dataMap.mapCol,r1, c1, r2, c2);
		}
		// Down
		else if(board.dataMap.stylePlay == 1)
		{
			move1(1,1,board.dataMap.mapRow, board.dataMap.mapCol,r1, c1, r2, c2);
		}
		// Left
		else if(board.dataMap.stylePlay == 2)
		{
			move2(1,1,board.dataMap.mapRow, board.dataMap.mapCol,r1, c1, r2, c2);
		}
		// Right
		else if(board.dataMap.stylePlay == 3)
		{
			move3(1,1,board.dataMap.mapRow, board.dataMap.mapCol,r1, c1, r2, c2);
		}
		// /2 vertical
		else if(board.dataMap.stylePlay == 5){
			moveTemp(style1, 1, 1, board.dataMap.mapRow/2, board.dataMap.mapCol, r1, c1, r2, c2);
			moveTemp(style2, board.dataMap.mapRow/2+1, 1, board.dataMap.mapRow, board.dataMap.mapCol, r1, c1, r2, c2);
		}
		// /2 hoz
		else if(board.dataMap.stylePlay == 6){			
			moveTemp(style1, 1, 1, board.dataMap.mapRow, board.dataMap.mapCol/2, r1, c1, r2, c2);
			moveTemp(style2, 1, board.dataMap.mapCol/2+1, board.dataMap.mapRow, board.dataMap.mapCol, r1, c1, r2, c2);
		}
		// /4
		else if(board.dataMap.stylePlay == 7){			
			moveTemp(style1, 1, 1, board.dataMap.mapRow/2, board.dataMap.mapCol/2, r1, c1, r2, c2);
			moveTemp(style2, board.dataMap.mapRow/2+1, board.dataMap.mapCol/2+1, board.dataMap.mapRow, board.dataMap.mapCol, r1, c1, r2, c2);
			moveTemp(style3, 1, board.dataMap.mapCol/2+1, board.dataMap.mapRow/2, board.dataMap.mapCol, r1, c1, r2, c2);
			moveTemp(style4, board.dataMap.mapRow/2+1, 1, board.dataMap.mapRow, board.dataMap.mapCol/2, r1, c1, r2, c2);
 
		}		
	}
	//=============UP========================================
	public void move0(int r0,int c0,int rMax, int cMax,int r1, int c1, int r2, int c2)
	{						
		boolean isIn1= false, isIn2 = false;
		if(r1>=r0&&r1<=rMax&&c1>=c0&&c1<=cMax)
			isIn1 = true;
		if(r2>=r0&&r2<=rMax&&c2>=c0&&c2<=cMax)
			isIn2 = true;
		if(c1!=c2)
		{			
			if(isIn1)
				applyMoveUp(r0,c0,rMax, cMax,c1, r1);
			if(isIn2)
				applyMoveUp(r0,c0,rMax, cMax,c2, r2);
		}
		else
		{			
			if(isIn1 && isIn2)
			applyMoveUp(r0,c0,rMax, cMax,c1, r2>r1?r2:r1);
		}
	}
	
	public void applyMoveUp(int r0, int c0, int rMax, int cMax, int c, int pivotRow)
	{	
		if(c>=c0 && c <= cMax && pivotRow>=r0 && pivotRow <= rMax)
		{			
		for(int row = pivotRow-1; row>=r0; row--)
		{						
			if(board.dataMap.map[row][c].type!=-1){
				int i = row+1;
				while (i<=pivotRow) {				
					if(board.bgPikachu.map[i][c]==-1)
					{					
						i++;
					}
					else{
						if(board.dataMap.map[i][c].type == -1)
							i++;
						else
							break;
					}
				}
				
				if(board.bgPikachu.map[i-1][c] == -1)
					continue;
				
				if(row == i-1)
				{					
				}
				else
				{					
					board.dataMap.map[i-1][c].set(board.dataMap.map[row][c].type);
					if(board.dataMap.map[row][c].isSelect){
						board.dataMap.map[i-1][c].setSelect(board.dataMap.map[row][c].isSelect);
						board.selectProcessor.item1 = board.dataMap.map[i-1][c];
					}
					board.dataMap.map[row][c].set(-1);
					board.dataMap.map[row][c].setSelect(false);
					board.dataMap.map[i-1][c].runFromTo(timeRun, new Vector2(board.dataMap.map[row][c].getX(), board.dataMap.map[row][c].getY()),
							new Vector2(board.dataMap.map[i-1][c].getX(), board.dataMap.map[i-1][c].getY()));
				}
			}
		}
		}
	}
	
	//=======================DOWN====================================

	public void move1(int r0, int c0, int rMax, int cMax,int r1, int c1, int r2, int c2)
	{	
		boolean isIn1= false, isIn2 = false;
		if(r1>=r0&&r1<=rMax&&c1>=c0&&c1<=cMax)
			isIn1 = true;
		if(r2>=r0&&r2<=rMax&&c2>=c0&&c2<=cMax)
			isIn2 = true;
		if(c1!=c2)
		{			
			if(isIn1)
				applyMoveDown(r0,c0,rMax, cMax,c1, r1);
			if(isIn2)
				applyMoveDown(r0,c0,rMax, cMax,c2, r2);
		}
		else
		{		
			if(isIn1 && isIn2)
				applyMoveDown(r0,c0,rMax, cMax,c1, r2<r1?r2:r1);
		}
	}
	public void applyMoveDown(int r0, int c0, int rMax, int cMax,int c, int pivotRow)
	{	
		for(int row = pivotRow+1; row<=rMax; row++)
		{			
			if(board.dataMap.map[row][c].type!=-1){			
				int i = row-1;
				while (i>=pivotRow) {				
					if(board.bgPikachu.map[i][c]==-1)
					{					
						i--;
					}
					else{
						if(board.dataMap.map[i][c].type == -1)
							i--;
						else
							break;
					}
				}
				
				if(board.bgPikachu.map[i+1][c] == -1)
					continue;
				
				if(row == i+1)
				{					
				}
				else
				{					
					board.dataMap.map[i+1][c].set(board.dataMap.map[row][c].type);
					if(board.dataMap.map[row][c].isSelect){
						board.dataMap.map[i+1][c].setSelect(board.dataMap.map[row][c].isSelect);
						board.selectProcessor.item1 = board.dataMap.map[i+1][c];
					}
					board.dataMap.map[row][c].set(-1);
					board.dataMap.map[row][c].setSelect(false);
					board.dataMap.map[i+1][c].runFromTo(timeRun, new Vector2(board.dataMap.map[row][c].getX(), board.dataMap.map[row][c].getY()),
							new Vector2(board.dataMap.map[i+1][c].getX(), board.dataMap.map[i+1][c].getY()));
				}
			}
		}
	}
	//=========================LEFT==================================
	public void move2(int r0, int c0, int rMax, int cMax,int r1, int c1, int r2, int c2) {
		boolean isIn1= false, isIn2 = false;
		if(r1>=r0&&r1<=rMax&&c1>=c0&&c1<=cMax)
			isIn1 = true;
		if(r2>=r0&&r2<=rMax&&c2>=c0&&c2<=cMax)
			isIn2 = true;
		if (r1 != r2) {
			if(isIn1)
				applyMoveLeft(r0,c0,rMax, cMax,r1,c1);
			if(isIn2)
				applyMoveLeft(r0,c0,rMax, cMax,r2,c2);
		} else {
			if(isIn1 && isIn2)
				applyMoveLeft(r0,c0,rMax, cMax,r1, c2 > c1 ? c2 : c1);
		}
	}

	public void applyMoveLeft(int r0, int c0, int rMax, int cMax,int r, int pivotCol) {
		for (int col = pivotCol - 1; col >=c0 ; col --) {
			if (board.dataMap.map[r][col].type != -1) {
				int i = col + 1;
				while (i <= pivotCol) {
					if (board.bgPikachu.map[r][i] == -1) {
						i++;
					} else {
						if(board.dataMap.map[r][i].type == -1){							
							i++;
						}
						else
							break;
					}
				}
				
				if(board.bgPikachu.map[r][i-1] == -1){
					continue;
				}
				
				if (col == i - 1) {
				} else {
					board.dataMap.map[r][i-1]
							.set(board.dataMap.map[r][col].type);
										
					if(board.dataMap.map[r][col].isSelect){
						board.dataMap.map[r][i-1].setSelect(board.dataMap.map[r][col].isSelect);
						board.selectProcessor.item1 = board.dataMap.map[r][i-1];
					}
					
					board.dataMap.map[r][col].set(-1);
					board.dataMap.map[r][col].setSelect(false);
					board.dataMap.map[r][i-1]
							.runFromTo(
									timeRun,
									new Vector2(
											board.dataMap.map[r][col]
													.getX(),
											board.dataMap.map[r][col]
													.getY()),
									new Vector2(
											board.dataMap.map[r][i-1]
													.getX(),
											board.dataMap.map[r][i-1]
													.getY()));
				}
			}
		}
	}
	//==================RIGHT==================================================
	public void move3(int r0, int c0, int rMax, int cMax,int r1, int c1, int r2, int c2) {
		boolean isIn1= false, isIn2 = false;
		if(r1>=r0&&r1<=rMax&&c1>=c0&&c1<=cMax)
			isIn1 = true;
		if(r2>=r0&&r2<=rMax&&c2>=c0&&c2<=cMax)
			isIn2 = true;
		if (r1 != r2) {
			if(isIn1)
				applyMoveRight(r0,c0,rMax, cMax,r1,c1);
			if(isIn2)
				applyMoveRight(r0,c0,rMax, cMax,r2,c2);
		} else {
			if(isIn1 && isIn2)
				applyMoveRight(r0,c0,rMax, cMax,r1, c2 < c1 ? c2 : c1);
		}
	}

	public void applyMoveRight(int r0, int c0, int rMax, int cMax,int r, int pivotCol) {		
		for (int col = pivotCol + 1; col <= cMax; col ++) {
			if (board.dataMap.map[r][col].type != -1) {
				int i = col - 1;
				while (i >= pivotCol) {
					if (board.bgPikachu.map[r][i] == -1) {
						i--;
					} else {
						if(board.dataMap.map[r][i].type==-1)
							i--;
						else{
							break;
						}
					}
				}			
				
				if(board.bgPikachu.map[r][i+1] == -1){
					continue;
				}
				
				if (col == i + 1) {
				} else {
					board.dataMap.map[r][i+1]
							.set(board.dataMap.map[r][col].type);
					if(board.dataMap.map[r][col].isSelect){
						board.dataMap.map[r][i+1].setSelect(board.dataMap.map[r][col].isSelect);
						board.selectProcessor.item1 = board.dataMap.map[r][i+1];
					}
					board.dataMap.map[r][col].set(-1);
					board.dataMap.map[r][col].setSelect(false);
					board.dataMap.map[r][i+1]
							.runFromTo(
									timeRun,
									new Vector2(
											board.dataMap.map[r][col]
													.getX(),
											board.dataMap.map[r][col]
													.getY()),
									new Vector2(
											board.dataMap.map[r][i+1]
													.getX(),
											board.dataMap.map[r][i+1]
													.getY()));
				}
			}
		}
	}
}
